<!DOCTYPE html>
<html lang="zh">
	<head>
		<meta charset="UTF-8">
		<meta http-equiv="X-UA-Compatible" content="IE=edge">
		<meta name="viewport" content="width=device-width, initial-scale=1.0">
		<title>数组处理</title>
		<style>
			* {
				padding: 0;
				margin: 0;
			}

			.page {
				background: purple;
				color: white;
				margin: 20px;
				padding: 20px;
			}

			h2,
			h3,
			p {
				margin: 20px 0;
			}
		</style>
	</head>
	<body>
		<div class="page">
			<h2>根据元素某个字段合并数组</h2>
			<h2></h2>
			<h2></h2>
			<h2></h2>
			<h2></h2>
		</div>
		<script>
			const arr = [{
					"timeCode": "1",
					"timeName": "1",
					"startTime": "08:00",
					"endTime": "08:50",
					"bigTime": "1"
				},
				{
					"timeCode": "2",
					"timeName": "2",
					"startTime": "09:00",
					"endTime": "09:50",
					"bigTime": "1"
				},
				{
					"timeCode": "3",
					"timeName": "3",
					"startTime": "10:00",
					"endTime": "10:50",
					"bigTime": "2"
				},
				{
					"timeCode": "4",
					"timeName": "4",
					"startTime": "11:00",
					"endTime": "11:50",
					"bigTime": "2"
				},
				{
					"timeCode": "5",
					"timeName": "Z1",
					"startTime": "12:00",
					"endTime": "12:50",
					"bigTime": "3"
				},
				{
					"timeCode": "6",
					"timeName": "Z2",
					"startTime": "13:00",
					"endTime": "13:50",
					"bigTime": "3"
				},
				{
					"timeCode": "7",
					"timeName": "5",
					"startTime": "14:00",
					"endTime": "14:50",
					"bigTime": "4"
				},
				{
					"timeCode": "8",
					"timeName": "6",
					"startTime": "15:00",
					"endTime": "15:50",
					"bigTime": "4"
				},
				{
					"timeCode": "9",
					"timeName": "9",
					"startTime": "16:00",
					"endTime": "16:50",
					"bigTime": "5"
				},
				{
					"timeCode": "10",
					"timeName": "10",
					"startTime": "17:00",
					"endTime": "17:50",
					"bigTime": "5"
				},
				{
					"timeCode": "11",
					"timeName": "11",
					"startTime": "18:00",
					"endTime": "18:50",
					"bigTime": "6"
				},
				{
					"timeCode": "12",
					"timeName": "12",
					"startTime": "19:00",
					"endTime": "19:50",
					"bigTime": "6"
				},
				{
					"timeCode": "13",
					"timeName": "13",
					"startTime": "20:00",
					"endTime": "20:50",
					"bigTime": "7"
				},
				{
					"timeCode": "14",
					"timeName": "14",
					"startTime": "21:00",
					"endTime": "21:50",
					"bigTime": "7"
				},
				{
					"timeCode": "15",
					"timeName": "15",
					"startTime": "22:00",
					"endTime": "22:50",
					"bigTime": "8"
				},
				{
					"timeCode": "16",
					"timeName": "16",
					"startTime": "23:00",
					"endTime": "23:50",
					"bigTime": "8"
				}
			];
			const array = [{
					"time": "3",
					"timeName": "3",
					"bigTime": "2"
				},
				{
					"time": "4",
					"timeName": "4",
					"bigTime": "2"
				},
				{
					"time": "5",
					"timeName": "Z1",
					"bigTime": "3"
				},
				{
					"time": "6",
					"timeName": "Z2",
					"bigTime": "3"
				}
			];
			//根据bigTime对数组进行合并数组
			const res = mergeByField(arr, 'bigTime', 'timeCode', ['timeCode', 'timeName'])
			console.log(res)
			const result = mergeByField(array, 'bigTime', 'time', ['time', 'timeName'])
			console.log(result)

			//两个数组合并
			const mergeArrays = (arr1, arr2, key = 'id') => [
				...arr1.map(item => ({
					...item,
					...arr2.find(innerItem => innerItem[key] === item[key])
				}))
			];

			// 使用示例
			const users = [{
				id: 1,
				name: 'Alice'
			}, {
				id: 2,
				name: 'Bob'
			}];
			const details = [{
				id: 1,
				age: 25
			}, {
				id: 2,
				age: 30
			}];
			const mArray = mergeArrays(users, details);
			// [
			//     {
			//         "id": 1,
			//         "name": "Alice",
			//         "age": 25
			//     },
			//     {
			//         "id": 2,
			//         "name": "Bob",
			//         "age": 30
			//     }
			// ]
			console.log(mArray);

			// 注意：后出现的属性会覆盖前面的同名属性
			let obj1 = {
				id: '1',
				name: '1111'
			}
			let obj2 = {
				id: '2',
				name: '22222222',
				age: 45
			}
			let obj3 = {
				...obj1,
				...obj2
			};
			console.log(obj3);

			//快速取整
			//const floor = Math.floor(4.9);
			const floor = ~~5.9;
			console.log(floor)

			//let pow = Math.pow(2,3);
			let pow = 5 ** 5; //5的5次方
			console.log(pow)

			let numbers = [1, 2, 3];
			// const doubled = numbers.map(e => {
			// 	return e * 2
			// })
			const doubled = numbers.map(e => e * 2)
			console.log(doubled)

			/**
			 * @param {Object} arr 数据源
			 * @param {Object} prop 合并的key
			 * @param {Object} checkKey 检查判断有误数据的key
			 * @param {Object} mergeArr 需要合并显示的字段
			 */
			function mergeByField(arr, prop, checkKey, mergeArr) {
				return arr.reduce((result, current) => {
					const lastItem = result[result.length - 1];
					if (lastItem && JSON.stringify(lastItem[prop]) === JSON.stringify(current[prop])) {
						mergeArr.forEach(e => {
							lastItem[e] += `,${current[e]}`
						})
					} else {
						if (current[checkKey]) {
							result.push({
								...current
							});
						}
					}
					return result;
				}, []);
			}

			//数组去重
			const unique = arr => [...new Set(arr)];
			console.log(unique([2, 4, 3, 1, 4, '2', 8, 4, 6, '7', 9]))

			const mixedArray = [2, 4, 3, 1, 4, '2', 8, 4, 6, '7', 9];
			const numberArray = mixedArray.map(item => Number(item));
			console.log(numberArray); // 输出: [2,4,3,1,4,2,8,4,6,7,9]

			const arrNum = [2, 4, 3, 1, 4, '2', 8, 4, 6, '7', 9].map(x => +x);
			console.log(arrNum)

			//数组求和
			const sum = arr => arr.reduce((acc, curr) => acc + curr, 0);
			console.log(sum([2, 4, 3, 1, 4, 2, 8, 4, 6, 7, 9]))

			//数组进行扁平化
			const flattenedArray = arr => arr.reduce((acc, curr) => acc.concat(curr), [])
			console.log(flattenedArray([2, 4, 3, 1, 4, 2, 8, 4, 6, 7, 9]))

			const fruit = {
				id: '123',
				name: '香蕉',
				type: 0
			}
			if (fruit.type) {
				console.log('存在')
			} else {
				console.log('不存在')
			}
			
			if (fruit.hasOwnProperty('type')) {
				console.log('存在')
			} else{
				console.log('不存在')
			}
			
			
		</script>
	</body>
</html>
